using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Xml.Serialization;
using CodePlex.DnD4eCampaignManager.DnD4e;
using CodePlex.DnD4eCampaignManager.DnD4e.Interfaces;
using CodePlex.DnD4eCampaignManager.Repositories.Interfaces;

namespace CodePlex.DnD4eCampaignManager.Repositories
{
    public class MonsterRepository : IMonsterRepository
    {
        private const string MONSTER_PATH_AND_EXTENSION = "Monster";

        public MonsterRepository()
        {
            if ( !Directory.Exists(string.Format("{0}s", MONSTER_PATH_AND_EXTENSION)) )
            {
                Directory.CreateDirectory(string.Format("{0}s", MONSTER_PATH_AND_EXTENSION));
            }
        }

        public ReadOnlyCollection<string> GetAllNames()
        {
            List<string> namesFromFileSystem = new List<string>();
            foreach ( string monster in Directory.GetFiles(string.Format("{0}s", MONSTER_PATH_AND_EXTENSION)) )
            {
                FileInfo fileInfo = new FileInfo(monster);
                if ( fileInfo.Name.Replace(fileInfo.Extension, string.Empty) != string.Empty )
                {
                    namesFromFileSystem.Add(fileInfo.Name.Replace(fileInfo.Extension, string.Empty));
                }
            }
            return namesFromFileSystem.AsReadOnly();
        }

        public IMonster GetByName(string playerCharacterName)
        {
            if ( playerCharacterName == string.Empty )
            {
                return null;
            }
            XmlSerializer xmlSerializer = new XmlSerializer(typeof ( Monster ));
            string monsterPath = string.Format("{1}s\\{0}.{1}", playerCharacterName, MONSTER_PATH_AND_EXTENSION);
            using ( TextReader textReader = new StreamReader(monsterPath) )
            {
                Monster monster = xmlSerializer.Deserialize(textReader) as Monster;
                if ( monster != null )
                {
                    monster.AttackPowers = monster.AttackPowers ?? string.Empty;
                    monster.Conditions = monster.Conditions ?? string.Empty;
                    monster.Tactics = monster.Tactics ?? string.Empty;
                    monster.SpecialQualities = monster.SpecialQualities ?? string.Empty;
                    monster.AttackPowers = monster.AttackPowers.Replace("\n", Environment.NewLine);
                    monster.Conditions = monster.Conditions.Replace("\n", Environment.NewLine);
                    monster.Tactics = monster.Tactics.Replace("\n", Environment.NewLine);
                    monster.SpecialQualities = monster.SpecialQualities.Replace("\n", Environment.NewLine);
                    monster.InitiativeScore = 0;
                    return monster;
                }
            }
            return null;
        }

        public void Save(IMonster playerCharacter)
        {
            if ( playerCharacter.Index.HasValue || playerCharacter.Name == string.Empty )
            {
                return;
            }
            XmlSerializer xmlSerializer = new XmlSerializer(typeof ( Monster ));
            using ( TextWriter textWriter =
                new StreamWriter(string.Format("{0}s\\{1}.{2}", MONSTER_PATH_AND_EXTENSION, playerCharacter.Name, MONSTER_PATH_AND_EXTENSION.ToLower()))
                )
            {
                xmlSerializer.Serialize(textWriter, playerCharacter);
            }
        }
    }
}